﻿using System;
using System.IO;

namespace OS.Http.Models
{
    /// <summary>
    /// 文件参数
    /// </summary>
    public struct FileParameter
    {
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name;

        /// <summary>
        /// 读写操作流，返回的是写入的字节流长度
        /// </summary>
        public Action<Stream> Writer;

        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName;

        /// <summary>
        /// 文件类型
        /// </summary>
        public string ContentType;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="fileStream">调用方自动释放</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="contentType">文件类型</param>
        public FileParameter(string name, Stream fileStream, string fileName, string contentType)
        {
            Writer = delegate(Stream s)
            {
                var array = new byte[1024];
                using (fileStream)
                {
                    var num = 0;
                    while ((num = fileStream.Read(array, 0, array.Length)) != 0)
                    {
                        s.Write(array, 0, num);
                    }
                }
            };
            FileName = fileName;
            ContentType = contentType;
            Name = name;
        }
    }
}